﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;

namespace web.admin.scTask
{
    public partial class InnerPar : System.Web.UI.Page
    {
        private ParticipantService participantService = new ParticipantService();
        private AcademyService academyService = new AcademyService();
        private ClassService classService = new ClassService();
        private StudentService studentService = new StudentService();
        private ContestService contestService = new ContestService();
        private GroupsService groupsService = new GroupsService();
        static bool isGroup;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string contestId = Request["id"];
                if (!String.IsNullOrEmpty(contestId))
                {
                    Rp_Bind(contestId);
                }
                else
                {
                    Rp_Bind(null);
                }

                BoContests contest = contestService.getContest(contestId);
                List<BoContests> contests = new List<BoContests>();
                contests.Add(contest);

                ContestsList.DataSource = contests;
                ContestsList.DataTextField = "CON_NAME";
                ContestsList.DataValueField = "CON_ID";
                ContestsList.DataBind();
                ContestsList.SelectedValue = contestId;
            }
        }


        /// <summary>
        /// repeater绑定
        /// </summary>
        /// <param name="conId">竞赛id</param>
        protected void Rp_Bind(object conId)
        {
            if (conId != null && !string.IsNullOrEmpty(conId.ToString()))
            {
                BLL.CompetitionService compertitionservice = new CompetitionService();
                BoCompetitions CompetitionsModel = compertitionservice.getCompetitionsByContest(Request.QueryString["id"]);
                if (CompetitionsModel != null)
                {
                    if (CompetitionsModel.COM_FORM == 0)//为个人报名
                    {
                        isGroup = false;
                        RpList.DataSource = participantService.GetContestParticipants(conId.ToString());
                        RpList.DataBind();
                        RpGroup.Visible = false;
                        RpList.Visible = true;
                        btnAddStudent.Visible = true;
                        btnAddGro.Visible = false;
                    }
                    else//为小组报名
                    {
                        isGroup = true;
                        RpGroup.DataSource = groupsService.getContestGroups(conId.ToString());
                        RpGroup.DataBind();
                        RpGroup.Visible = true;
                        RpList.Visible = false;
                        btnAddGro.Visible = true;
                        btnAddStudent.Visible = false;
                    }
                }
            }
            else
            {
                RpList.DataSource = participantService.getAllParticipants();
                RpList.DataBind();
            }
        }
        protected void ContestsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selContestsId = ContestsList.SelectedValue;
            IList<BoParticipants> comps = new List<BoParticipants>();
            if (selContestsId == "全部竞赛")
            {
                comps = participantService.getAllParticipants();
            }
            else
            {
                comps = participantService.GetContestParticipants(selContestsId);
            }
            RpList.DataSource = comps;
            RpList.DataBind();
        }


        /// <summary>
        /// 添加学生点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddStudent_Click(object sender, EventArgs e)
        {

            BLL.CompetitionService compertitionservice = new CompetitionService();
            BoCompetitions CompetitionsModel = compertitionservice.getCompetitionsByContest(Request.QueryString["id"]);
                if (CompetitionsModel.COM_FORM == 0)//为个人报名
                {
                    Response.Redirect("Participants/addStudent.aspx?ContestId=" + ContestsList.SelectedValue);
                }
                else
                {
                    Response.Write("<script>alert('此竞赛为小组报名，不允许单独添加学生')</script>");
                }

          
        }

        /// <summary>
        /// 添加小组事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddGro_Click(object sender, EventArgs e)
        {
            BLL.CompetitionService compertitionservice = new CompetitionService();
            BoCompetitions CompetitionsModel = compertitionservice.getCompetitionsByContest(Request.QueryString["id"]);
            if (CompetitionsModel.COM_FORM == 1)
            {
                Response.Redirect("Participants/addGrop.aspx?ContestId=" + ContestsList.SelectedValue);
            }
            else
            {
                Response.Write("<script>alert('此竞赛为个人报名，不允许添加小组')</script>");
            }
            
        }

        protected void btnGro_Click(object sender, EventArgs e)
        {
            Response.Redirect("Participants/GroupList.aspx?ContestId=" + ContestsList.SelectedValue);

        }
        /// <summary>
        /// repeater事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Rp_Event(object sender, RepeaterCommandEventArgs e)
        {
            if (!isGroup)
            {
                string par_id = ((HiddenField)e.Item.FindControl("Hf_id")).Value;
                BoParticipants participantmodel = new BoParticipants();
                participantmodel.PAR_ID = par_id;
                switch (e.CommandName)
                {
                    case "par_Del":
                        participantService.RemoveParticipant(participantmodel);
                        break;
                }
                Rp_Bind(Request["id"]);
            }
            else
            {
                string GroupId = ((HiddenField)e.Item.FindControl("Hf_id")).Value;
                BoGroups GroupModel = new BoGroups();
                GroupModel.GRO_ID = GroupId;
                switch (e.CommandName)
                {
                    case "par_Del":
                        groupsService.DeleteGroup(GroupModel);
                        break;
                }
                Rp_Bind(Request["id"]);
            }
        }
        protected void BatchDelete(object sender, EventArgs e)
        {
            int j = 0;
            for (int i = 0; i < RpList.Items.Count; i++)
            {
                string id = ((HiddenField)RpList.Items[i].FindControl("Hf_id")).Value;
                CheckBox cb = (CheckBox)RpList.Items[i].FindControl("CbChoose");
               
                BoContests ccc = contestService.getContest(id);
                if (cb.Checked)
                {
                    contestService.DeleteContest(ccc);
                    j++;
                }
            }
            Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='InnerPar.aspx'; </script>");
            return;
        }
    }
}